<?php

namespace app\common\model;

use think\Model;

/**
 * vod模型
 */
class Vod extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    // 时间字段取出后的默认时间格式
    protected $dateFormat = 'Y-m-d H:i:s';
    // 追加属性
    protected $append = [];

    const VOD_STATUS_NO_UPLOAD = 0;
    const VOD_STATUS_UPLOADED = 1;
    const VOD_STATUS_PUBLISHED = 2;
    const VOD_STATUS_DISABLED = 3;

    public function video()
    {
        return $this->belongsTo('Video', 'video_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function videolist()
    {
        return $this->belongsTo('Videolist', 'videolist_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 新增vod信息
     * @param $videoId
     * @param $videolistId
     * @param $videoName
     * @param string $videoType
     * @param null $assetId
     * @return array|bool|float|int|mixed|object|\stdClass|null
     */
    public static function addVod($videoId, $videolistId, $videoName, $videoType = 'MP4', $assetId = null, $url = null, $guid = null)
    {
        $vod = self::where(['video_id' => $videoId])->find();
        if ($vod) {
            $attr['video_name'] = $videoName;
            $attr['video_type'] = $videoType;
            if (!empty($assetId)) {
                $attr['asset_id'] = $assetId;
                $attr['url'] = $url;
                $attr['status'] = self::VOD_STATUS_UPLOADED;
            } else if (!empty($guid)) {
                $attr['guid'] = $guid;
                $attr['url'] = $url;
                $attr['status'] = self::VOD_STATUS_UPLOADED;
            } else {
                $attr['status'] = self::VOD_STATUS_NO_UPLOAD;
            }
            self::where(['id' => $vod['id']])->update($attr);
        } else {
            $vod = new self;
            $vod->video_id = $videoId;
            $vod->videolist_id = $videolistId;
            $vod->video_name = $videoName;
            $vod->video_type = $videoType;
            if (!empty($assetId)) {
                $vod->asset_id = $assetId;
                $vod->url = $url;
                $vod->status = self::VOD_STATUS_UPLOADED;
            } else if (!empty($guid)) {
                $vod->guid = $guid;
                $vod->url = $url;
                $vod->status = self::VOD_STATUS_UPLOADED;
            }
            $vod->save();
            $vodId = $vod->id;
        }
        return $vodId;
    }

    /**
     * 新增vod信息
     * @param $videoId
     * @param $videolistId
     * @param $videoName
     * @param string $videoType
     * @param null $uid
     * @param null $url
     * @param int $status
     * @return array|bool|float|int|mixed|object|\stdClass|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function addVodViaCloudFlare($videoId, $videolistId, $videoName, $videoType = 'MP4', $uid = null, $url = null, $status = Vod::VOD_STATUS_NO_UPLOAD)
    {
        $vod = self::where(['video_id' => $videoId, 'platform' => 'cloudflare'])->find();
        if ($vod) {
            $attr['video_name'] = $videoName;
            $attr['video_type'] = $videoType;
            if (!empty($uid)) {
                $attr['asset_id'] = $uid;
                $attr['url'] = $url;
            }
            $attr['status'] = $status;
            self::where(['id' => $vod['id']])->update($attr);
            $vodId = $vod['id'];
        } else {
            $vod = new self;
            $vod->video_id = $videoId;
            $vod->videolist_id = $videolistId;
            $vod->video_name = $videoName;
            $vod->video_type = $videoType;
            if (!empty($uid)) {
                $vod->asset_id = $uid;
                $vod->url = $url;
            }
            $vod->status = $status;
            $vod->platform = 'cloudflare';
            $vod->save();
            $vodId = $vod->id;
        }
        return $vodId;
    }

    /**
     * 更新vod信息
     * @param null $assetId
     * @param $attr
     * @return array|bool|float|int|mixed|object|\stdClass|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function updateVodViaAssetId($assetId, $attr)
    {
        $vod = self::where(['asset_id' => $assetId])->find();
        if ($vod) {
            self::where(['id' => $vod['id']])->update($attr);
        }
        return $vod;
    }

    /**
     * 更新vod信息
     * @param null $assetId
     * @param $attr
     * @return array|bool|float|int|mixed|object|\stdClass|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function updateVod($id, $attr)
    {
        $vod = self::where(['id' => $id])->find();
        if ($vod) {
            self::where(['id' => $vod['id']])->update($attr);
        }
        return $vod;
    }
}
